package com.ohs.bll.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ohs.bll.dto.SearchResultDTO;
import com.ohs.bll.service.LocationService;
import com.ohs.dal.dao.AccountDAO;
import com.ohs.dal.dao.LocationDAO;
import com.ohs.dal.dao.RoomDAO;
import com.ohs.dal.model.Account;
import com.ohs.dal.model.Location;
import com.ohs.dal.model.Province;
import com.ohs.dal.model.Room;
import com.ohs.dal.model.Specialty;

public class LocationServiceImpl implements LocationService {

	LocationDAO locationDAO;
	RoomDAO roomDAO;
	AccountDAO accountDAO;

	public List<Location> findLocation(String keyword) {
		return locationDAO.findLocation(keyword);
	}
	
	public List<Location> list() {
		return locationDAO.list();
	}

	public void update(Location location) {
		locationDAO.update(location);
	}

	public Location get(Long id) {
		return locationDAO.get(id);
	}

	public Location getByAccount(String username) {
		return locationDAO.getByAccount(username);
	}

	public int searchTotal(String keyword, long selectedProvinceId, long selectedSpecialtyId) {
		return locationDAO.searchTotal(keyword, selectedProvinceId, selectedSpecialtyId);
	}
	
	public List<SearchResultDTO> search(String keyword, int startIndex, int pageSize, long selectedProvinceId, long selectedSpecialtyId) {
		List<Location> locations = locationDAO.search(keyword, startIndex, pageSize, selectedProvinceId, selectedSpecialtyId);
		List<SearchResultDTO> result = new ArrayList<SearchResultDTO>();
		if (locations != null && locations.size() > 0) {
			for (Location location : locations) {
				SearchResultDTO dto = new SearchResultDTO(location);
				boolean hasAvailableRoom = false;
				if (location.getAccount() != null) {
					Account account = accountDAO.get(location.getAccount().getUsername());
					List<Room> rooms = roomDAO.findByLocaiton(location.getLocationId());
					if (rooms.size() > 0) {
						for(Room room :rooms) {
							if (room.getTurnOn()) {
								hasAvailableRoom = true;
							}
						}
					}
					dto.setDoctorName(account.getName());
					dto.setUsername(account.getUsername());
				}
				if (hasAvailableRoom) {
					dto.setTurnOnBooking(true);
				}
				
				dto.setSpecialties(getSpecialties(location));
				dto.setAddress(location.getAddress()  + ", " + location.getProvince().getName());
				result.add(dto);
			}
		}
		
		return result;
	}
	
	public LocationDAO getLocationDAO() {
		return locationDAO;
	}

	public void setLocationDAO(LocationDAO locationDAO) {
		this.locationDAO = locationDAO;
	}

	public RoomDAO getRoomDAO() {
		return roomDAO;
	}

	public void setRoomDAO(RoomDAO roomDAO) {
		this.roomDAO = roomDAO;
	}

	public AccountDAO getAccountDAO() {
		return accountDAO;
	}

	public void setAccountDAO(AccountDAO accountDAO) {
		this.accountDAO = accountDAO;
	}

	public SearchResultDTO getFullDetail(String username) {
		Location location = locationDAO.getByAccount(username);
		SearchResultDTO result = new SearchResultDTO(location);
		Account account = accountDAO.get(username);
		
		result.setSpecialties(getSpecialties(location));
		result.setDoctorName(account.getName());
		result.setUsername(account.getUsername());
		result.setAddress(location.getAddress()  + ", " + location.getProvince().getName());
		return result;
	}
	
	private String getSpecialties(Location location) {
		StringBuffer specialties = new StringBuffer();
		if (location.getSpecialty1() != null) {
			specialties.append(location.getSpecialty1().getName() + ", ");
		}
		if (location.getSpecialty2() != null) {
			specialties.append(location.getSpecialty2().getName() + ", ");
		}
		if (location.getSpecialty3() != null) {
			specialties.append(location.getSpecialty3().getName() + ", ");
		}
		if (location.getSpecialty4() != null) {
			specialties.append(location.getSpecialty4().getName() + ", ");
		}
		if (location.getSpecialty5() != null) {
			specialties.append(location.getSpecialty5().getName() + ", ");
		}
		return (specialties.length() > 0) ? specialties.substring(0, specialties.toString().length() - 2) : "";
	}
	
	public List<Province> getAllProvince() {
		return locationDAO.getAllProvince();
	}

	public SearchResultDTO getById(Long id) {
		Location location = locationDAO.get(id);
		SearchResultDTO result = new SearchResultDTO(location);
		result.setSpecialties(getSpecialties(location));
		return result;
	}

	public List<Location> getAllLocation() {
		return locationDAO.getAllLocation();
	}
	
	public List<Specialty> getAllSpecialty() {
		return accountDAO.getAllSpecialty();
	}
	
	public List<Location> getTopLocation(int max) {
		return locationDAO.getTopLocation(max);
	}
}